package indi.zn.commons.easiness

import java.lang.reflect.Field

import com.fasterxml.jackson.databind.ObjectMapper
import com.google.common.collect.Lists

import indi.zn.commons.collection.Maps
import indi.zn.commons.loader.ClassScaner

class EntityModelPrinter {
	
	private static ObjectMapper objectMapper=new ObjectMapper();
	
	def static scaner=new ClassScaner();
	
		
	def static void scanPackage(String packageStr) {
		

		def cls=scaner.doScan(packageStr);
		
		for (class1 in cls) {
			
			println class1
			
			printColumn(class1);
			
		}
		
	}
	
	def static void printColumn(Class clazz) {
		
		List<Map<String, Object>> list=[];
		
		List<Map<String, Object>> gridcols=[["name": "id", "hidden":"true"]];
		
		
	Field[] fields=clazz.getDeclaredFields();
		
	for ( field in fields) {
		
		String  name=field.getName();
		
		Map<String, Object> map= [:];
	
	if(name=="status") {
		
		map.put("key",name);
		map.put("name", "状态");
		map.put("type", "select");
		map.put("dateType", "json");
		map.put("dataSource", [["name":"有效","value":"valid"],["name":"作废","value":"invalid"]]);
		
	}	else {
		
		map.put("key",name);
		map.put("name", "");
		
	 if (field.getType().equals(Date.class)) {
		
		 map.put("type", "time");
		 
	}
	 
	}
		
		list.add(map);
		
	Map<String, Object> map2= [:];
	
	map2.put("sortable","false");
	map2.put("label", "");
	map2.put("name", name);
	map2.put("align", "center");
	
	if(name=="status") {
		
		map2.put("formatter", "function(val, index, rowData){var status='';if(val == 'valid') status='有效';	if(val == 'invalid') status='作废';return status;}");
		
	}
	
	gridcols.add(map2);

	}
	
	try {
		
		println "model:"
		println objectMapper.writeValueAsString(list)
		
		println "grid:"
		
		println objectMapper.writeValueAsString(gridcols);
		
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		
	}

}
